package datastructure.line.queue;

import datastructure.line.Array.Array;

public class LoopQueue<E> implements Queue<E> {
    private Array<E> array;
    private int[] data;
    /**
     * 队首位置
     */
    private int front;
    /**
     * 队尾位置
     */
    private int tail;
    /**
     * 元素个数
     */
    private int size;

    public LoopQueue() {
        this(10);
    }

    public LoopQueue(int capacity) {
        data = new int[capacity + 1];
        front = 0;
        tail = 0;
        size = 0;
    }

    @Override
    public void enqueue(E e) {
        if (tail++ % array.getCapacity() == front) {
            throw new IllegalArgumentException("full");
        }
        array.add(tail, e);
        tail++;
        size++;
    }

    @Override
    public E dequeue() {
        if (array.isEmpty()) {
            throw new IllegalArgumentException("empty");
        }
        E elem = array.remove(front);
        front++;
        return elem;
    }

    @Override
    public E getFront() {
        return array.get(front);
    }

    @Override
    public int getSize() {
        return 0;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    public int getCapacity() {
        return data.length - 1;
    }
}
